package cn.lx.config;

import cn.lx.dao.*;
import cn.lx.utils.RRException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * 数据库配置
 *
 * @author lixin
 */
@Configuration
public class DbConfig {
    @Value("${generator.database: mysql}")
    private String database;
    @Autowired
    private MySQLGeneratorDao mySQLGeneratorDao;
    @Autowired
    private OracleGeneratorDao oracleGeneratorDao;
    @Autowired
    private SQLServerGeneratorDao sqlServerGeneratorDao;
    @Autowired
    private PostgreSQLGeneratorDao postgreSQLGeneratorDao;


    private final static String MYSQL = "mysql";
    private final static String ORACLE = "oracle";
    private final static String SQLSERVER = "sqlserver";
    private final static String POSTGRESQL = "postgresql";


    @Bean
    @Primary
    public GeneratorDao getGeneratorDao() {
        if (MYSQL.equalsIgnoreCase(database)) {
            return mySQLGeneratorDao;
        } else if (ORACLE.equalsIgnoreCase(database)) {
            return oracleGeneratorDao;
        } else if (SQLSERVER.equalsIgnoreCase(database)) {
            return sqlServerGeneratorDao;
        } else if (POSTGRESQL.equalsIgnoreCase(database)) {
            return postgreSQLGeneratorDao;
        } else {
            throw new RRException("不支持当前数据库：" + database);
        }
    }
}
